package com.xxxx.server.controller;


import com.alibaba.excel.EasyExcel;
import com.xxxx.server.config.ExcelConfig;
import com.xxxx.server.page.RespPageBean;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.*;
import com.xxxx.server.service.impl.DepartmentServiceImpl;
import com.xxxx.server.service.impl.EmployeeServiceImpl;
import com.xxxx.server.utils.SendMailUtil;
import com.xxxx.server.utils.UploadDataListener;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {

//    @Autowired
//    private IEmployeeService iEmployeeService;
    @Autowired
    private EmployeeServiceImpl iEmployeeService;
    @Autowired
    private IPositionService positionService;
    @Autowired
    private INationService nationService;
    @Autowired
    private IJoblevelService joblevelService;
    @Autowired
    private DepartmentServiceImpl departmentService;
    @Resource
    private IPoliticsStatusService politicsStatusService;



    /**
     * 展示员工数据信息
     * @param currentPage  当前页
     * @param size    显示页大小
     * @param employee   员工对象
     * @param beginDateScope  开始日期范围
     * @return
     */
    //进入员工页面 展示员工数据信息
    @GetMapping("/")
    @ApiOperation(value = "查询员工对象")
    public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer size, Employee employee, Date[] beginDateScope) {
        return iEmployeeService.getEmployeeByPage(currentPage, size, employee,beginDateScope);
    }

    /**
     * 添加员工
     * @param
     * @return
     */
    //添加员工信息
    @PostMapping("/")
    @ApiOperation(value = "添加员工信息")
    public RespBean addEmployee(@RequestBody EmployeeDataBase employeeDataBase){
        //service 层做校验
        if(iEmployeeService.saveEmployee(employeeDataBase) == 1){
            //调用添加邮件的方法
            SendMailUtil.sendEmail(employeeDataBase.getEmail(),employeeDataBase);
            return RespBean.success("添加成功,且邮件已发送！");
        }
        return RespBean.error("添加失败！！！");
    }




   /**
    * 更新  所有接口
    * */
   //更新员工信息
   @PutMapping("/")
   @ApiOperation("跟新员工信息")
   public RespBean updateEmployee(@RequestBody Employee employee){
       if (iEmployeeService.updateEmployeeById(employee) == 1){
           return RespBean.success("更新成功");
       }
       return RespBean.error("更新失败");
   }
    @GetMapping("/positions")
    @ApiOperation("查询所有职位")
    public List<Position> position(){
        List<Position> list = positionService.list();
        return list;
    }
    @GetMapping("/nations")
    @ApiOperation("查询所有名族")
    public List<Nation> nations(){
        List<Nation> list = nationService.list();
        return list;
    }
    @GetMapping("/politicsstatus")
    @ApiOperation("查询所有名族")
    public List<PoliticsStatus> politicsstatus(){
        List<PoliticsStatus> list = politicsStatusService.list();
        return list;
    }

    @GetMapping("/joblevels")
    @ApiOperation("工作水品")
    public List<Joblevel> joblevels(){
        List<Joblevel> list = joblevelService.list();
        return list;
    }
    @GetMapping("/deps")
    @ApiOperation("部门信息")
    public List<Department> deps(){
        List<Department> list = departmentService.getAll();
        return list;
    }
    @GetMapping("/maxWorkID")
    @ApiOperation("获取id  +1 赋值给新进员工")
    public RespBean maxWorkID(){
        RespBean respBean = RespBean.build().setCode(200).setObj(String.format("%08d", iEmployeeService.maxWorkID() + 1));
        return respBean;
    }


    /**
     * 删除
     * @param id
     * @return
     */
    //删除员工(ok)
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工")
    public RespBean deleteEmployeeById(@PathVariable Integer id){
        if(iEmployeeService.deleteEmployeeById(id) == 1){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }


    /**
     * 导入导出数据
     * @return
     */
    //导出数据
    @GetMapping("/export")
    public void download(HttpServletResponse response)throws IOException {
        List<EmployeeDataBase> employees = iEmployeeService.QueryAllEmployee();

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Employee.class).sheet("模板").registerConverter(new ExcelConfig()).doWrite(employees);
    }

    //导入数据
    @PostMapping("/import")
    @ResponseBody
    public RespBean upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Employee.class, new UploadDataListener(iEmployeeService)).registerConverter(new ExcelConfig()).sheet().doRead();
        return RespBean.success("导入成功");
    }
}



